<?php
class Product_home_model extends CI_Model{
    
    public function list_pro()
    {
        $sql = "SELECT *
                FROM tbl_product
                    INNER JOIN tbl_pro_img
                        ON tbl_product.pro_id = tbl_pro_img.pro_id 
                        WHERE pro_img_main=1 
                        ORDER BY pro_date DESC";
        $query = $this->db->query($sql);
        return $query->result_array(); 
    }
    public function get_info($proid)
    {
        $sql = "SELECT *
                FROM tbl_product
                    INNER JOIN tbl_pro_img
                        ON tbl_product.pro_id = tbl_pro_img.pro_id
                    INNER JOIN tbl_brand
                        ON tbl_product.bra_id = tbl_brand.bra_id
                WHERE  tbl_product.pro_id = {$proid} AND pro_img_main = 1 ";
        $query = $this->db->query($sql);
        return $query->result_array();
    }
    public function get_imgs($proid)
    {
        $sql = "SELECT tbl_pro_img.pro_img_src
                FROM tbl_product
                    INNER JOIN tbl_pro_img
                        ON tbl_product.pro_id = tbl_pro_img.pro_id
                WHERE  tbl_product.pro_id = {$proid} AND pro_img_main != 1 LIMIT 0,10";
        $query = $this->db->query($sql);
        return $query->result_array();
    }

    public function brand_pro($brand_id, $number = 0, $num_per_page = '', $sort = '')
    {
        $limit = '';
        $sort = "ORDER BY tbl_product.pro_date DESC";
        if($num_per_page != '')
        {
            $limit = " LIMIT ".$number.",".$num_per_page;
        }
        
        $sql = "SELECT *
                FROM tbl_product
                INNER JOIN tbl_pro_img
                    ON tbl_product.pro_id = tbl_pro_img.pro_id
                WHERE bra_id = {$brand_id} AND pro_img_main = 1"." ".$sort." ".$limit;
        
        $query = $this->db->query($sql);
        return $query->result_array();
    
    }
    public function cate_pro($cate_id, $number = 0, $num_per_page = '', $sort = '')
    {
        $sort = "ORDER BY tbl_product.pro_date DESC";
        $limit = '';
        if($num_per_page != '')
        {
            $limit = " LIMIT ".$number.",".$num_per_page;
        }
        
        $list_child = $this->category_home_model->list_child($cate_id);
        $list_sub = $list_child.$cate_id;
        //echo $list_sub;
        $sql = "SELECT DISTINCT tbl_product.pro_name, tbl_product.pro_info,tbl_product.pro_id,tbl_product.pro_date, tbl_product.pro_price, tbl_product.pro_sale, tbl_cate_pro.pro_id, tbl_pro_img.pro_id, tbl_pro_img.pro_img_main, tbl_pro_img.pro_img_src
                FROM tbl_product
                INNER JOIN tbl_pro_img
                    ON tbl_product.pro_id = tbl_pro_img.pro_id
                INNER JOIN tbl_cate_pro
                    ON tbl_product.pro_id = tbl_cate_pro.pro_id
                WHERE cate_id IN ($list_sub) AND pro_img_main = 1"." ".$sort." ".$limit;
        
        $query = $this->db->query($sql);
        return $query->result_array();
    
    }
    public function list_range_price($start, $end)
    {
        $sql = "SELECT *
                FROM tbl_product
                    INNER JOIN tbl_pro_img
                        ON tbl_product.pro_id = tbl_pro_img.pro_id 
                        WHERE tbl_product.pro_sale <= {$end} AND tbl_product.pro_sale >= {$start} AND pro_img_main=1";
        $query = $this->db->query($sql);
        return $query->result_array();
    }
    public function insert_rating($pro_id = '', $rating = '')
    {
        $sql = "INSERT INTO tbl_rating(pro_id, rating)VALUES({$pro_id},{$rating})";
        $this->db->query($sql);
    }
    public function get_rating_avg($pro_id = '')
    {
        $sql = "SELECT AVG(rating) as avg_rate FROM tbl_rating WHERE pro_id = {$pro_id}";
        $query = $this->db->query($sql);
        return $query->result_array();
    }
    public function insert_comment($pro_id = '', $cus_name = '', $comment ='')
    {
        $sql = "INSERT INTO tbl_comment(pro_id, cus_name, comment)VALUES({$pro_id},'{$cus_name}', '{$comment}')";
        $this->db->query($sql);
    }
    public function list_comment($pro_id = '')
    {
        $sql = "SELECT * FROM tbl_comment WHERE pro_id = {$pro_id} ORDER BY id DESC";
        $query = $this->db->query($sql);
        return $query->result_array();
    }
    public function list_seller()
    {
    $sql = "SELECT tbl_product.pro_id, pro_name, pro_sale, tbl_pro_img.pro_img_src, sum(quatity) as qty FROM `tbl_order_detail`
                INNER JOIN tbl_product
                    ON  tbl_product.pro_id = tbl_order_detail.pro_id
                INNER JOIN tbl_pro_img
                    ON  tbl_pro_img.pro_id = tbl_product.pro_id
                WHERE tbl_pro_img.pro_img_main = 1
                GROUP BY tbl_order_detail.pro_id
                
                ORDER BY qty DESC
                LIMIT 0,5";
        $query = $this->db->query($sql);
        return $query->result_array();
    }
}
